package com.ryx.star.dao;

import com.ryx.star.po.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface BlogRepository extends JpaRepository<Blog,Long>, JpaSpecificationExecutor<Blog> {


    Blog getBlogById(Long id);

    @Query("select b from Blog b where b.recommend =true ")
    List<Blog> findTop(Pageable pageable);

    @Query("select b from Blog b where b.title like ?1 or b.description like ?1")
    Page<Blog> findByQuery(String query,Pageable pageable);

    @Transactional
    @Modifying
    @Query("update Blog b set b.views=b.views+1 where b.id=?1")
    int updateViews(Long id);


    @Query(value = "select  date_format(b.update_time,'%Y') as year from t_blog b group by year order by year desc",nativeQuery = true)
    List<String> findGroupYear();

    @Query(value = "select * from t_blog b where date_format(b.update_time,'%Y') =?",nativeQuery = true)
    List<Blog> findByYear(String year);

}
